package com.ruoyi.activity.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.activity.model.domain.User;
import com.ruoyi.activity.model.dto.ImportUserTemplateRequest;
import com.ruoyi.activity.model.dto.QueryUserRequest;
import com.ruoyi.activity.service.IUserService;
import com.ruoyi.common.exception.base.BaseException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户信息管理，存储用户及相关详情Controller
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/activity/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 查询用户信息管理，存储用户及相关详情列表
     */
    @PreAuthorize("@ss.hasPermi('activity:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 管理端条件分页查询
     */
    @PreAuthorize("@ss.hasPermi('activity:user:list')")
    @GetMapping("/admin/list")
    public TableDataInfo adminList(QueryUserRequest queryUserRequest) {
        startPage();
        User user = new User();
        if (queryUserRequest != null) {
            user.setIdentity(queryUserRequest.getIdentity());
            user.setSchool(queryUserRequest.getSchool());
            user.setEnrollmentGroup(queryUserRequest.getEnrollmentGroup());
        }
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户信息管理，存储用户及相关详情列表
     */
    @PreAuthorize("@ss.hasPermi('activity:user:export')")
    @Log(title = "用户信息管理，存储用户及相关详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户信息管理，存储用户及相关详情数据");
    }

    /**
     * 获取用户信息管理，存储用户及相关详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户信息管理，存储用户及相关详情
     */
    @PreAuthorize("@ss.hasPermi('activity:user:add')")
    @Log(title = "用户信息管理，存储用户及相关详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        String userAccount = user.getAccount();
        User accountUser = userService.selectUserByAccount(userAccount);
        if (accountUser != null) {
            throw new BaseException("该账号已存在");
        }
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户信息管理，存储用户及相关详情
     */
    @PreAuthorize("@ss.hasPermi('activity:user:edit')")
    @Log(title = "用户信息管理，存储用户及相关详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户信息管理，存储用户及相关详情
     */
    @PreAuthorize("@ss.hasPermi('activity:user:remove')")
    @Log(title = "用户信息管理，存储用户及相关详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * 账号上传
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "账号上传", businessType = BusinessType.IMPORT)
    @PostMapping("/importUserData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ImportUserTemplateRequest> util = new ExcelUtil<>(ImportUserTemplateRequest.class);
        List<ImportUserTemplateRequest> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = userService
                .importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importUserTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ImportUserTemplateRequest> util = new ExcelUtil<>(ImportUserTemplateRequest.class);
        util.importTemplateExcel(response, "项目账号数据");
    }
}
